package com.songxh.application.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.mysql.cj.util.LogUtils;
import com.songxh.application.api.BlogHomePageService;
import com.songxh.application.dto.home.ArticleDTO;
import com.songxh.application.dto.home.HotTagsDTO;
import com.songxh.core.entity.UserContext;
import com.songxh.core.page.PageData;
import com.songxh.core.utils.LoginUtils;
import com.songxh.domain.service.api.LikeService;
import com.songxh.domain.service.api.TagService;
import com.songxh.infrastructure.db.mapper.ArticleMapper;
import com.songxh.infrastructure.db.mapper.TagMapper;
import com.songxh.infrastructure.db.pojo.Like;
import com.songxh.infrastructure.db.pojo.Tag;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author songxh
 * @create 2022-12-19-15:54
 */
@Service
@Slf4j
public class BlogHomePageServiceImpl implements BlogHomePageService {

    @Autowired
    private TagService tagService;

    @Autowired
    private ArticleMapper articleMapper;

    @Autowired
    private TagMapper tagMapper;

    @Autowired
    private LikeService likeService;

    /**
     * 文章首页数据显示
     * @param pageData
     * @return
     */
    @Override
    public PageData<ArticleDTO> articleList(PageData pageData) {
        //获取用户id
        UserContext userContext = LoginUtils.get();
        Long userId = userContext.getId();

        if (pageData.isPage()) {
            PageHelper.startPage(pageData.getPage(), pageData.getPageSize(), true);
        }
        Map<String, Object> params = pageData.getParams();
        Page<ArticleDTO> articleData = articleMapper.getArticleData(params);

        articleData.forEach(e -> {
            List<Long> tagIds = articleMapper.getTags(e.getArticleId());
            ArrayList<String> list = new ArrayList<>();
            for (Long tagId : tagIds) {
                String tagName = tagService.lambdaQuery().eq(Tag::getId, tagId).one().getTagName();
                list.add(tagName);
            }
            e.setTags(list);

            //获取点赞情况
            List<Like> likes = likeService.lambdaQuery()
                    .eq(Like::getArticleId, e.getArticleId())
                    .eq(Like::getUserId, userId)
                    .eq(Like::getCommentId, "0")//如果是给文章点赞，那么表中的评论id为默认值 0
                    .list();
            if (CollectionUtils.isNotEmpty(likes)){
                e.setLiked("1");
            }else {
                e.setLiked("0");
            }
        });

        pageData.setResult(articleData);
        pageData.setTotal((int) articleData.getTotal());
        return pageData;
    }

    /**
     * 获取最热标签
     * @return
     */
    @Override
    public List<HotTagsDTO> getHotTags() {

        //----todo 暂定显示4个，可修改
        Long level = 4l;
        List<HotTagsDTO> tagsDTOS = tagMapper.getHotTagIds(level);
        return tagsDTOS;
    }
}
